package com.centling.util;

import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

/**
 * 短信发送 by John
 */
public class SmsSender {

	public static Logger log = Logger.getLogger(SmsSender.class);

	/**
	 * http://114.255.71.158:8061/?username=qdhl&password=123456&message=ceshi&phone=15863050069&epid=109499&linkid=&subcode=
	 */
	@Value("${smssender.smsurl}")
	private static String smsUrl;
	
	@SuppressWarnings("deprecation")
	public static Integer send(String phone,String message) {
		try {
			String paramStr = "?username=qdhl&password=123456&message=" + URLEncoder.encode(message, "gb2312") 
					+ "&phone=" + phone 
					+ "&epid=109499&linkid=&subcode=";
			URL url = new URL(smsUrl+paramStr);
			log.info("目标手机号:		" + phone);
			log.info("短信内容:		" + message);
			log.info("发送短信的URL是:	" + url);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.connect();
			DataInputStream in = new DataInputStream(connection.getInputStream());
			String inline = in.readLine();
			in.close();
			connection.disconnect();
			log.info("短信平台的返回值是 : 	" + inline);
			return Integer.valueOf(inline);
		} catch (Exception ex2) {
			log.error(" exe IOException:" + ex2.getMessage());
		}
		return -1;
	}
}
